package com.waymap.service.impl;

import com.github.pagehelper.PageHelper;
import com.waymap.common.vo.ItemsVo;
import com.waymap.common.vo.SkuItemVo;
import com.waymap.domain.*;
import com.waymap.mapper.ItemsMapper;
import com.waymap.mapper.LocationBinsMapper;
import com.waymap.service.ItemService;
import com.waymap.service.LocationBinsService;
import com.waymap.utils.CommonEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LocationBinsServiceImpl extends BaseService<LocationBins> implements LocationBinsService {

    @Resource
    private LocationBinsMapper locationBinsMapper;


    @Override
    public ResponseEntity<Map> addLocationBins(Map locationBins) {
        try {
            LocationBins locatBins = new LocationBins();
            locatBins.setAccountId((String) locationBins.get("accountId"));
            locatBins.setBinId((String) locationBins.get("binId"));
            locatBins.setBinName((String) locationBins.get("binName"));
            locatBins.setLocationId((String) locationBins.get("locationId"));
            if (!Optional.ofNullable(locationBins.get("binType").toString()).equals("") && !locationBins.get("binType").toString().equals("0")) {
                locatBins.setBinType((String) locationBins.get("binType"));
            }
            Optional<LocationBins> judgeLocationBins = Optional.ofNullable(locationBinsMapper.selectOne(locatBins));
            if (judgeLocationBins.isPresent()) {
                locatBins.setId(judgeLocationBins.get().getId());
                locationBinsMapper.updateByPrimaryKey(locatBins);
            } else {
                locationBinsMapper.insert(locatBins);
            }
            Map map = new HashMap();
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
            return ResponseEntity.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            Map map = new HashMap();
            map.put("status", CommonEnum.value("eFail"));
            map.put("msg", e.getMessage());
            return ResponseEntity.ok(map);
        }

    }

    @Override
    public ResponseEntity<Map> selectRecommondBins(RecomDto recomDto) {
        /**
         *   查询推荐条数 为3条
         *   每个库位Sku 数量不能超过3
         */
        Map map = new HashMap();
        RecommendBinsVo recommendBinsVo = new RecommendBinsVo();
        try {
            /*查询所有满足条件的库位 排除某个库位3个sku限制*/
            List<BinsVo> binVoList = locationBinsMapper.selcctRecommendBin(recomDto);
            /*历史库位  一个库位上有1个或2个sku*/
            List<BinsVo> allHistoryBin = binVoList
                    .stream()
                    .filter(e ->
                            e.getSkuTotal() <= 3)
                    .filter(e -> e.getSkuTotal() > 0)
                    .collect(Collectors.toList());
            /*统计符合条件的库位 (还要进过数量筛选)*/
            List<BinsVo> historyBins = new ArrayList<>();
            /*sku使用过的库位*/
            List<ItemBinDto> itemBinDtos = locationBinsMapper.selectBinBySku(recomDto);
            if(itemBinDtos.size()>0){
                for (int i = 0; i < itemBinDtos.size(); i++) {
                    ItemBinDto itemBinDto = itemBinDtos.get(i);
                    if(allHistoryBin.size()>0){
                        for (int j = 0; j < allHistoryBin.size(); j++) {
                            BinsVo binsVo = allHistoryBin.get(j);
                            if(itemBinDto.getBinName().equals(binsVo.getBinName())){
                                historyBins.add(binsVo);
                            }
                        }
                    }
                }
            }
            /*打乱集合*/
            Collections.shuffle(historyBins);
            List<BinsVo> historyBin = historyBins.stream().limit(3).collect(Collectors.toList());
           // log.info("历史库位： {}",historyBin.size());
            /*判断 如果历史库位<2 空库位就推荐2个 负责为1个 */
            int zeroBinNum = 1;
            if(historyBin.size()<2){
                zeroBinNum=2;
            }
            /*打乱集合*/
            Collections.shuffle(binVoList);
            /*取出空库位*/
            List<BinsVo> zeroBin = binVoList.stream().filter(e -> e.getSkuTotal() == 0).limit(zeroBinNum).collect(Collectors.toList());
            //log.info("空库位： {}",zeroBin.size());
            recommendBinsVo.setHistoryBins(historyBin);
            recommendBinsVo.setZeroBins(zeroBin);
            map.put("result", recommendBinsVo);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
        } catch (Exception e) {
            log.error("查询推荐库位错误： {}", e.getMessage());
        }
        return ResponseEntity.ok(map);
    }
}
